package com.apptest.android.core.network;

import com.apptest.android.core.bean.ArchitectureBean;
import com.apptest.android.core.bean.ArticleItemBean;
import com.apptest.android.core.bean.ArticleListBean;
import com.apptest.android.core.bean.BannerBean;
import com.apptest.android.core.bean.NavigationTagBean;
import com.apptest.android.core.bean.UserBean;
import com.apptest.android.core.bean.WxBloggerBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ApiEngine {
    private static ApiService apiService = RetrofitServiceManager
            .getInstance()
            .create(ApiService.class);

    //插入观察者
    private static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())//子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())//主线程回调
                .subscribe(observer);
    }

    public static void getArticleListData(int pageNum, Observer<BaseResponse<ArticleListBean>> observer) {
        setSubscribe(apiService.getArticleListData(pageNum),observer);
    }

    public static void getBannerData(Observer<BaseResponse<List<BannerBean>>> observer) {
        setSubscribe(apiService.getBannerData(),observer);
    }

    public static void getTopArticles(Observer<BaseResponse<List<ArticleItemBean>>> observer) {
        setSubscribe(apiService.getTopArticles(),observer);
    }

    public static void login(String username, String password, Observer<BaseResponse<UserBean>> observer) {
        setSubscribe(apiService.login(username,password),observer);
    }

    public static void getNavigationData(Observer<BaseResponse<List<NavigationTagBean>>> observer) {
        setSubscribe(apiService.getNavigationData(),observer);
    }

    public static void getArchitectureTreeData(Observer<BaseResponse<List<ArchitectureBean>>> observer) {
        setSubscribe(apiService.getArchitectureTreeData(),observer);
    }

    public static void getArchitectureArticles(int page, int cid, Observer<BaseResponse<ArticleListBean>> observer) {
        setSubscribe(apiService.getArchitectureArticles(page, cid),observer);
    }

    public static void getWxBloggerList(Observer<BaseResponse<List<WxBloggerBean>>> observer) {
        setSubscribe(apiService.getWxBloggerList(),observer);
    }

    public static void getWxArticles(int id, int page, Observer<BaseResponse<ArticleListBean>> observer) {
        setSubscribe(apiService.getWxArticles(id, page),observer);
    }
}
